package com.sky.service.impl;

import com.alibaba.druid.sql.PagerUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 套餐业务实现
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    //user/SetmealController
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<SetmealVO> list(Setmeal setmeal) {
        //根据状态查询出所有套餐
        List<Setmeal> list = setmealMapper.list(setmeal);
        //创建一个装所有套餐的VO对象的集合
        List<SetmealVO> setmealVOList = new ArrayList<>();
        //遍历所有的套餐
        for (Setmeal setmeal1 : list) {
            //把所有套餐都封装到VO对象中
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal1, setmealVO);
            //把所有的VO对象都放入VO集合中
            setmealVOList.add(setmealVO);
        }
        //把装有所有VO对象的List集合返回
        return setmealVOList;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    //admin/SetmealController
    /*
     * 新增套餐
     * */
    public void saveWhithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);
    }
    /*
     * 套餐分页查询
     * @param setmealPageQueryDTO
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealPageQueryDTO, setmeal);

        Page<Setmeal> page = setmealDishMapper.pageQuery(setmeal);
        return new PageResult(page.getTotal(), page.getResult());
    }
    /*
     * 套餐批量删除
     * @param ids
     * @return
     */
    public void deleteBatch(List<Long> ids) {

    }
    /*
     * 根据id查询套餐，用于修改页面回显数据
     * @param id
     * @return
     * */
    public SetmealVO getByIdWithDish(Long id) {
        return null;
    }
    /*
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    public void update(SetmealDTO setmealDTO) {

    }
    /*
     * 套餐起售停售
     * @param status
     * @param id
     * @return
     */
    public void startOrStop(Integer status, Long id) {

    }



}
